#!/usr/bin/env python
import sys
import re
import urllib2
from cStringIO import StringIO
from gzip import GzipFile
import bz2

while(1):
  l = sys.stdin.readline()
  if l == '':
    break
  if re.search('http://', l) and not re.match('#', l):
    (deb, uri, dist, components) = l.strip().split(' ', 3)
    for comp in components.split(' '):
      suffix = '.bz2'
      try:
        if deb == 'deb':
          url = '%s/dists/%s/%s/binary-i386/Packages%s' % (uri, dist,
                      comp, suffix)
        elif deb == 'deb-src':
          url = '%s/dists/%s/%s/source/Sources%s' % (uri, dist, comp,
                      suffix)
        else:
          sys.stderr.write('Error parsing deb string\n')
        res = urllib2.urlopen(url).read()
        file = bz2.decompress(res)
      except urllib2.HTTPError:
        suffix = '.gz'
        try:
          if deb == 'deb':
            url = '%s/dists/%s/%s/binary-i386/Packages%s' % (uri, dist,
                        comp, suffix)
          elif deb == 'deb-src':
            url = '%s/dists/%s/%s/source/Sources%s' % (uri, dist, comp,
                      suffix)
          else:
            sys.stderr.write('Error parsing deb string\n')
          res = urllib2.urlopen(url).read()
          file = GzipFile('', 'r', 0, StringIO(res)).read()
        except urllib2.HTTPError:
          suffix = ''
          try:
            if deb == 'deb':
              url = '%s/dists/%s/%s/binary-i386/Packages%s' % (uri, dist,
                                comp, suffix)
            elif deb == 'deb-src':
              url = '%s/dists/%s/%s/source/Sources%s' % (uri, dist, comp,
                            suffix)
            else:
              sys.stderr.write('Error parsing deb string\n')
            res = urllib2.urlopen(url).read()
            file = res
          except urllib2.HTTPError:
            sys.stderr.write('Cannot open file: %s\n' % url);
  
      entry = dict()
      key = ''
  
      for line in file.splitlines():
        if line == '' and not deb == 'deb-src':
          format = """<iq type='set'
    from='FIXME'
    to='FIXME'
    id='FIXME'>
  <pubsub xmlns='http://jabber.org/protocol/pubsub'>
    <publish node='%s'>
      <item id='%s'>
        <entry
xmlns='http://jabber.org/protocol/fileshare#item_meta-data'>
          <size>%s</size>
          <modified>FIXME</modified>
          <checksum
type="md5">%s</checksum>
          <mime>application/x-deb</mime>
          <description>%s</description>
          <mirrors>
            <mirror type='http'
                    address='FIXME'
                    ref='%s'/>
          </mirrors>
        </entry>
      </item>
    </publish>
  </pubsub>
</iq>
"""
          sys.stdout.write(format % (entry['package'], entry['version'],
            entry['size'], entry['md5sum'], entry['description'], entry['filename']))
          entry.clear()
        elif re.match('^[ \t]', line):
          line = re.sub('^[ \t]\.', '', line)
          line = re.sub('^[ \t]', '', line)
          entry[key] += '\n' + line
        else:
          (key, value) = line.split(': ', 1)
          key = key.lower()
          entry[key] = value
